 unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,Uni,UniConnct,UniConfig,Class_WAGE_FL_OLD,
  Dialog_View, MySQLUniProvider, UniProvider, SQLServerUniProvider,Class_WAGE_FL_NEW;

type
  TForm1 = class(TDialogView)
    Btnx_Read: TButton;
    Btnx_Writ1: TButton;
    Btnx_Writ2: TButton;
    SQLServerUniProvider1: TSQLServerUniProvider;
    MySQLUniProvider1: TMySQLUniProvider;
    Memo1: TMemo;
    Btnx_Const: TButton;
    procedure Btnx_ReadClick(Sender: TObject);
    procedure Btnx_Writ1Click(Sender: TObject);
    procedure Btnx_ConstClick(Sender: TObject);
    procedure Btnx_Writ2Click(Sender: TObject);
  private
    FListData1:TStringList;//*list of *TWAGEFLOLD
    FListData2:TStringList;//*list of *TWAGEFLOLD
  protected
    procedure SetInitialize;override;
    procedure SetCommParams;override;
    procedure SetGridParams;override;
    procedure SetComboItems;override;
    procedure TryFreeAndNil;override;
  public
    procedure OnUniConnctCustomConnectionEventByOBJ(AConnectionMark:string;var OBJA:TUniConfig);
  end;

var
  Form1: TForm1;

const
  CONST_MARK_MSSQL='MSSQL';
  CONST_MARK_MYSQL='MYSQL';

implementation

uses
  Class_KzDebug,Class_KzUtils;

{$R *.dfm}

procedure TForm1.Btnx_ConstClick(Sender: TObject);
var
  I:Integer;
  SQLA:string;
  TMPA:string;
  TMPB:string;
  TMPX:string;
  UniConnct:TUniConnection;
  UniDataSet:TUniQuery;
begin
  try
    UniConnct:=UniConnctEx.GetConnection(CONST_MARK_MSSQL);
    //-<



    UniDataSet:=TWAGEFLOLD.GetDataSet('SELECT TOP  1  * FROM TBL_WAGE_FL',UniConnct);
    for I := 0  to UniDataSet.Fields.Count-1 do
    begin
      TMPA:=UniDataSet.Fields.Fields[I].FieldName;
      TMPB:=StringReplace(TMPA,'_','',[rfReplaceAll]);
      TMPX:='if ParamName=%S then ' + #13 +
            'begin' + #13 +
              'Param.Value:=%S;' + #13 +
            'end else';
      TMPX:=Format(TMPX,[QuotedStr(TMPA),TMPB]);
      Memo1.Lines.Add(TMPX);
    end;
    //->
  finally
    FreeAndNil(UniConnct);
    FreeAndNil(UniDataSet);
  end;
end;

procedure TForm1.Btnx_ReadClick(Sender: TObject);
var
  SQLA:string;
  UniConnct:TUniConnection;
begin
  try
    UniConnct:=UniConnctEx.GetConnection(CONST_MARK_MSSQL);
    //-<
    if FListData1=nil then
    begin
      FListData1:=TStringList.Create;
    end;
    if FListData2=nil then
    begin
      FListData2:=TStringList.Create;
    end;
    SQLA:='SELECT TOP 10000 * FROM TBL_WAGE_FL';
    KzDebug.Started;
    KzDebug.TickFmt('V1',[]);
    TWAGEFLOLD.ListDB(SQLA,UniConnct,FListData1);
    TWAGEFLNEW.ListDB(SQLA,UniConnct,FListData2);
    KzDebug.TickFmt('V2',[]);
    //->
  finally
    FreeAndNil(UniConnct);
  end;
end;

procedure TForm1.Btnx_Writ1Click(Sender: TObject);
var
  I:Integer;
  SQLA:string;
  Objt:TWAGEFLOLD;
  UniConnct:TUniConnection;
begin
  if (FListData1=nil) or (FListData1.Count=0) then Exit;

  try
    UniConnct:=UniConnctEx.GetConnection(CONST_MARK_MYSQL);
    try
      UniConnct.StartTransaction;
      //-<
      TWAGEFLOLD.ExecuteSQL('TRUNCATE TABLE TBL_WAGE_FL',UniConnct);
      KzDebug.Started;
      KzDebug.TickFmt('W1',[]);
      for I := 0 to FListData1.Count-1 do
      begin
        Objt:=TWAGEFLOLD(FListData1.Objects[I]);
        if Objt=nil then Continue;

        Objt.InsertDB(UniConnct);
      end;
      KzDebug.TickFmt('W2',[]);
      //->
      UniConnct.Commit;
    except
      on E:Exception do
      begin
        UniConnct.Rollback;
        raise Exception.Create(E.Message);
      end;
    end;
  finally
    FreeAndNil(UniConnct);
  end;

  ShowMessage('writ1 is over.');
end;

procedure TForm1.Btnx_Writ2Click(Sender: TObject);
var
  I:Integer;
  SQLA:string;
  Objt:TWAGEFLNEW;
  UniConnct:TUniConnection;
begin
  if (FListData2=nil) or (FListData2.Count=0) then Exit;

  try
    UniConnct:=UniConnctEx.GetConnection(CONST_MARK_MYSQL);
    try
      UniConnct.StartTransaction;
      //-<
      TWAGEFLOLD.ExecuteSQL('TRUNCATE TABLE TBL_WAGE_FL',UniConnct);
      KzDebug.Started;
      KzDebug.TickFmt('X1',[]);
      for I := 0 to FListData2.Count-1 do
      begin
        Objt:=TWAGEFLNEW(FListData2.Objects[I]);
        if Objt=nil then Continue;

        Objt.InsertDB(UniConnct);
      end;
      KzDebug.TickFmt('X2',[]);
      //->
      UniConnct.Commit;
    except
      on E:Exception do
      begin
        UniConnct.Rollback;
        raise Exception.Create(E.Message);
      end;
    end;
  finally
    FreeAndNil(UniConnct);
  end;

  ShowMessage('writ1 is over.');
end;

procedure TForm1.OnUniConnctCustomConnectionEventByOBJ(AConnectionMark: string;
  var OBJA: TUniConfig);
begin
  if AConnectionMark=CONST_MARK_MSSQL then
  begin
    OBJA.UnixType:=UniConfig.CONST_PROVIDER_SQLSRV;
    OBJA.UnixServ:='.';
    OBJA.UnixUser:='sa';
    OBJA.UnixPswd:='sa';
    OBJA.DataBase:='YZTF_PRMS_0713';
  end else
  if AConnectionMark=CONST_MARK_MYSQL then
  begin
    OBJA.UnixType:=UniConfig.CONST_PROVIDER_MYSQLX;
    OBJA.UnixServ:='localhost';
    OBJA.UnixUser:='root';
    OBJA.UnixPswd:='root';
    OBJA.DataBase:='db_demo';
  end;
end;

procedure TForm1.SetComboItems;
begin
  inherited;

end;

procedure TForm1.SetCommParams;
begin
  inherited;

end;

procedure TForm1.SetGridParams;
begin
  inherited;

end;

procedure TForm1.SetInitialize;
begin
  inherited;
  UniConnctEx.ConnctMode:=cmByOBJ;
  UniConnctEx.OnUniConnctCustomConnectionEventByOBJ:=self.OnUniConnctCustomConnectionEventByOBJ;
end;

procedure TForm1.TryFreeAndNil;
begin
  inherited;
  if FListData1<>nil then TKzUtils.TryFreeAndNil(FListData1);
  if FListData2<>nil then TKzUtils.TryFreeAndNil(FListData2);
end;

end.

